package com.jigsaw.controller;

import com.jigsaw.model.MoviesCateEntity;
import com.jigsaw.model.MoviesInfoEntity;
import com.jigsaw.model.UserEntity;
import com.jigsaw.repository.MoviesRepository;
import com.jigsaw.repository.UserRepository;
import com.jigsaw.util.TokenProcessor;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.Date;

/**
 * Created by Jigsaw on 2016/3/13.
 */
@Controller
public class LoginController {
    private static Logger log = LoggerFactory.getLogger(LoginController.class);

    @Autowired
    private UserRepository userRepository;



    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String test() {
        System.out.print("1211111111111111111111");
        return "index";
    }

    @RequestMapping(value = "/api/v1/register", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String registerUser(@ModelAttribute("UserEntity") UserEntity user) {
        String email = user.getEmail();
        String username = user.getUsername();
        UserEntity checkUser = userRepository.findByUsername(username);
        UserEntity checkEmail = userRepository.findByEmail(email);
        if (checkUser != null || checkEmail != null) {
            throw new Movies401Exception();
        }
        JSONObject status = new JSONObject();
        try {
            status.put("code", 200);
            status.put("msg", "register success");
            user.setViewTime(0);
            String encodePwd = TokenProcessor.getInstance().encodeSHA1(user.getPassword());
            user.setPassword(encodePwd);
            userRepository.saveAndFlush(user);
        } catch (JSONException e) {
            throw new Movies500Exception();
        }
        return status.toString();
    }

    @RequestMapping(value = "/api/v1/login", method = RequestMethod.POST, produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String login(@ModelAttribute("UserEntity") UserEntity user) throws JSONException {
        String email = user.getEmail();
        String password = user.getPassword();
        String encodePwd = TokenProcessor.getInstance().encodeSHA1(password);
        UserEntity checkEmail = userRepository.findByEmail(email);
        if (checkEmail == null || !checkEmail.getPassword().equals(encodePwd)) {
            throw new Movies401Exception();
        } else {
            Integer vTime = checkEmail.getViewTime() + 1;
//            user.setViewTime(vTime);
            Timestamp now = new Timestamp((new Date().getTime()));
//            user.setLastTime(now);
            String tk = TokenProcessor.getInstance().generateToken();
            userRepository.updateUser(vTime, now, tk, email);
            UserEntity userEntity = userRepository.findByEmail(email);
            JSONObject userJson = new JSONObject();
            userJson.put("uid", userEntity.getUid());
            userJson.put("username", userEntity.getUsername());
            userJson.put("email", userEntity.getEmail());
            userJson.put("avatar", userEntity.getAvatar());
            userJson.put("private_token", userEntity.getPrivateToken());
            userJson.put("view_time", userEntity.getViewTime());
            return userJson.toString();
        }
    }

}
